package com.geekbang.work2;

import java.io.File;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @author nhsoft.lul
 */
public class LoadDataApplication {

    static String URL = "jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8&serverTimezone=UTC&useSSL=false";
    static String USER = "root";
    static String PASSWORD = "123456";

    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement statement = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(URL, USER, PASSWORD);

            String sql = "delete from book_order";
            statement = conn.prepareStatement(sql);
            statement.execute();

            long start = System.currentTimeMillis();
            conn.setAutoCommit(false);
            //文件在resource下面
            sql = "load data local infile 'C:/Users/lgh_l/Desktop/data.csv' into table book_order " +
                    "FIELDS TERMINATED BY ',' " +
                    "OPTIONALLY ENCLOSED BY '' " +
                    "ESCAPED BY '\\\\'  " +
                    "LINES TERMINATED BY '\\n' (order_num, customer_num, order_total_money, order_discount_money, order_pay_money);";

            statement = conn.prepareStatement(sql);
            statement.execute();
            conn.commit();
            conn.setAutoCommit(true);
            long end = System.currentTimeMillis();
            System.out.println("耗时:" + (end - start));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (statement != null) {
                    statement.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
